package geektime.spring.springbucks.customerservice;

import geektime.spring.springbucks.customerservice.support.CustomConnectionKeepAliveStrategy;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import jdk.nashorn.internal.ir.ReturnNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Slf4j
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class CustomerServiceApplication {

  public static void main(String[] args) {
    SpringApplication.run(CustomerServiceApplication.class, args);
  }

  @Bean
  public CloseableHttpClient httpClient() {
    return HttpClients.custom()
        .setConnectionTimeToLive(30, TimeUnit.SECONDS)
        .evictIdleConnections(30, TimeUnit.SECONDS)
        .setMaxConnTotal(200)
        .setMaxConnPerRoute(20)
        .disableAutomaticRetries()
        .setKeepAliveStrategy(new CustomConnectionKeepAliveStrategy())
        .build();
  }

}
